$(function() {

	var ajaxLoaderImg = new Image();
	ajaxLoaderImg.src = "/i/ajax-loader-small.gif";
	var ajaxLoader = '<img id="ajaxLoader" src="/i/ajax-loader-small.gif" width="16" height="16" alt="" />';

	var visaSenatorServiceId = $('#visa-senator-service-id');
	var countryId = $('#country-id').val();

	var aOrderVisa = $('#a-order-visa');
	var aOrderVisaHref = aOrderVisa.attr('href');

	var requirementsBlock = $('#requirements-block');

	visaSenatorServiceId.change(function() {
		$this = $(this);
		$this.after(ajaxLoader);
		$.ajax({
			async   : true,
			type    : "GET",
			url     : "/a_search/visa.getVisaSenatorServiceInfo",
			dataType: "json",
			data    : {country_id: countryId, service_id: $this.val()},
			success : function(data){
				if (data.error == 0) {
					$('#requirements').html(data.requirements);
					var newAOrderVisaHref = aOrderVisaHref.replace(/service_id=(.*?)$/i, 'service_id=' + $this.val());
					aOrderVisa.attr('href', newAOrderVisaHref);
					requirementsBlock.show();
				} else if (data.error == 1) {
					requirementsBlock.hide();
				}
				$('#ajaxLoader').remove();
			},
			error : function() {
				alert('Error with requesting ajax data.');
			}
		});
	});
	
	$('#visa-locations li a').bind('click', function() {
		var $this = $(this);
		var continent = $this.parents('li').attr('class');
		$('#visa-locations li a.active').removeClass('active');
		$this.addClass('active');
		if ($('#' + continent).is(':hidden')) {
			$('div.countries-holder:visible').fadeOut(100);
			setTimeout(function() {
				$('#' + continent).fadeIn();
			}, 100);
		}
		$('#visa-country-description').hide();
		$('div.countries-holder ul li a.active').removeClass('active');
	});
});